package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.IvgTextAdapter;
import com.hxkcloud.nurse.adapter.KindApater;
import com.hxkcloud.nurse.adapter.NewsListAdapter;
import com.hxkcloud.nurse.beans.ArticleBean;
import com.hxkcloud.nurse.beans.ArticleKindBean;
import com.hxkcloud.nurse.beans.ArticleKindChildBean;
import com.hxkcloud.nurse.beans.ArticleWholeBean;
import com.hxkcloud.nurse.dborm.ArticleKindDao;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/12.
 * 资讯列表
 */
public class NewNewsActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener, IvgTextAdapter.ResultSelectedListener, NewsListAdapter.ResultSelectedListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private ImageView iv_search;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<ArticleBean> newsInfoList;
    private NewsListAdapter adapter;

    private RecyclerView mRecyclerView;
    private IvgTextAdapter mAdapter;
    private List<ArticleKindBean> kindNews;//分类

    private LinearLayout kindLayout;
    private TextView kind1TV;
    private TextView kind2TV;
    private TextView kind3TV;
    private TextView kind4TV;
    private ImageView kind1IV;
    private ImageView kind2IV;
    private ImageView kind3IV;
    private ImageView kind4IV;
    private LinearLayout ll_kind1;
    private LinearLayout ll_kind2;
    private LinearLayout ll_kind3;
    private LinearLayout ll_kind4;

    private PopupWindow kindPopup;//选择分类
    private ListView kindListView;
    private KindApater kindAdapter;

    private View kindWholeView;

    private TextView kindText;

    private String type = "";

    private ArticleWholeBean resultBean;

    private List<ArticleKindChildBean> kindChilds;

    private int pageIndex = 1;
    private int pageSize = 15;

    private boolean isLoadMore = false;// 是否加载更多

    private int totalCount = 0;

    private boolean isChoose = false;

    private boolean isKind1 = false;
    private boolean isKind2 = false;
    private boolean isKind3 = false;
    private boolean isKind4 = false;

    private boolean[] isKinds;

    private View view1, view2, view3;

    private boolean isChoosed1 = false;
    private boolean isChoosed2 = false;
    private boolean isChoosed3 = false;
    private boolean isChoosed4 = false;

    private int curTitle = -1;

    private String selectChildId = "";
    private String kindTitle;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_ARTICLELIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ArticleWholeBean) msg.obj;
                    totalCount = resultBean.getTotalCount();
                    if (resultBean.getList() != null) {

                        if (resultBean.getList().size() == 0) {
                            if (isLoadMore) {
                                refreshLayout
                                        .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            } else {
                                newsInfoList.clear();
                                adapter.setData(newsInfoList);
                                refreshLayout
                                        .refreshFinish(PullToRefreshLayout.SUCCEED);
                            }
                        } else {
                            if (isLoadMore) {

                            } else {
                                newsInfoList.clear();
                            }
                            newsInfoList.addAll(resultBean.getList());
                            adapter.setData(newsInfoList);
                            if (isLoadMore) {
                                refreshLayout
                                        .loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            } else {
                                refreshLayout
                                        .refreshFinish(PullToRefreshLayout.SUCCEED);
                            }

                        }

                    } else {
                        newsInfoList.clear();
                        adapter.setData(newsInfoList);
                    }
                }

                break;
            case MessageCode.NETWORKCODE_ARTICLELIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_news);
        newsInfoList = new ArrayList<ArticleBean>();

        if (this.getIntent().hasExtra("type")) {
            type = this.getIntent().getStringExtra("type");
        }
        isChoose = this.getIntent().getBooleanExtra("choose", false);

        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        iv_search = (ImageView) findViewById(R.id.iv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("文章");

        iv_search.setVisibility(View.VISIBLE);
        iv_search.setImageResource(R.mipmap.nav_search);
        iv_back.setOnClickListener(this);
        iv_search.setOnClickListener(this);

        kindLayout = (LinearLayout) findViewById(R.id.news_layout_kind);
        kind1TV = (TextView) findViewById(R.id.news_tv_kind1);
        kind2TV = (TextView) findViewById(R.id.news_tv_kind2);
        kind3TV = (TextView) findViewById(R.id.news_tv_kind3);
        kind4TV = (TextView) findViewById(R.id.news_tv_kind4);
        kind1IV = (ImageView) findViewById(R.id.news_iv_kind1);
        kind2IV = (ImageView) findViewById(R.id.news_iv_kind2);
        kind3IV = (ImageView) findViewById(R.id.news_iv_kind3);
        kind4IV = (ImageView) findViewById(R.id.news_iv_kind4);
        ll_kind1 = (LinearLayout) findViewById(R.id.news_layout_kind1);
        ll_kind2 = (LinearLayout) findViewById(R.id.news_layout_kind2);
        ll_kind3 = (LinearLayout) findViewById(R.id.news_layout_kind3);
        ll_kind4 = (LinearLayout) findViewById(R.id.news_layout_kind4);
        view1 = (View) findViewById(R.id.news_view_1);
        view2 = (View) findViewById(R.id.news_view_2);
        view3 = (View) findViewById(R.id.news_view_3);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.news_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.news_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        adapter = new NewsListAdapter(this, newsInfoList, recycleView, this, screenWidth, type);
        recycleView.setAdapter(adapter);

        mRecyclerView = (RecyclerView) findViewById(R.id.news_recyclerview_horizontal);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        kindText = (TextView) findViewById(R.id.news_kind_text);

        initKindPopup();

    }

    private void initKindPopup() {
        kindPopup = new PopupWindow(this);
        View kindView = LayoutInflater.from(this).inflate(R.layout.popup_layout_kind, null);
        kindPopup.setContentView(kindView);

        kindPopup.setWidth(screenWidth);
        kindPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        kindPopup.setFocusable(true);
        kindPopup.setTouchable(true);
        kindPopup.setOutsideTouchable(true);
        kindPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        if (kindChilds == null) {
            kindChilds = new ArrayList<ArticleKindChildBean>();
        } else {
            kindChilds.clear();
        }
//        if(kindNews!=null){

//         for(int i = 0;i < kindNews.get(position).getChildList().size();i++){
//             System.out.println("kindNews.get(position).getChildList()==="+kindNews.get(position).getChildList());
//             ArticleKindChildBean bean =  kindNews.get(position).getChildList().get(i);
//             kindChilds.add(bean);
//         }

//        }

        kindListView = (ListView) kindView.findViewById(R.id.kind_listview);
        kindAdapter = new KindApater(this, kindChilds);
        kindListView.setAdapter(kindAdapter);
        kindListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                kindText.setText(kindChilds.get(position).getName());

                for (int i = 0; i < kindChilds.size(); i++) {
                    if (i == position) {
                        kindChilds.get(i).setIsChoosed(true);
                        selectChildId = kindChilds.get(position).getItemId();
                    } else {
                        kindChilds.get(i).setIsChoosed(false);
                    }
                }
                if (isKind1) {
                    isChoosed1 = true;
                    isChoosed2 = false;
                    isChoosed3 = false;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind2) {
                    isChoosed1 = false;
                    isChoosed2 = true;
                    isChoosed3 = false;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind3) {
                    isChoosed1 = false;
                    isChoosed2 = false;
                    isChoosed3 = true;
                    isChoosed4 = false;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind4IV.setImageResource(R.mipmap.tab_arrow);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                } else if (isKind4) {
                    isChoosed1 = false;
                    isChoosed2 = false;
                    isChoosed3 = false;
                    isChoosed4 = true;
                    kind1IV.setImageResource(R.mipmap.tab_arrow);
                    kind2IV.setImageResource(R.mipmap.tab_arrow);
                    kind3IV.setImageResource(R.mipmap.tab_arrow);
                    kind4IV.setImageResource(R.mipmap.tab_arrow_s);
                    kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                    kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                }

                if (kindNews.size() > 4) {
                    for (int i = 0; i < isKinds.length; i++) {
                        if (isKinds[i]) {
                            curTitle = i;
                            break;
                        }
                    }
                }
                loading();
                YiJiaHuNetWorkUtils.newarticleList(loginInfo.getDepartmentId(), "", kindChilds.get(position).getItemId(), type, pageIndex, pageSize, handler);
                kindPopup.dismiss();
            }
        });

        kindWholeView = (View) kindView.findViewById(R.id.kind_view_whole);
        kindWholeView.setOnClickListener(this);

        kindPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (kindNews.size() > 4) {
                    if(curTitle==-1){
                        for (int i = 0; i < kindNews.size(); i++) {
                            kindNews.get(i).setIsSelected(false);
                        }
                    }else{
                        for (int i = 0; i < kindNews.size(); i++) {
                            if(i==curTitle){
                                kindNews.get(curTitle).setIsSelected(true);
                            }else{
                                kindNews.get(i).setIsSelected(false);
                            }

                        }

                    }
                    mAdapter.setData(kindNews);
                }

                if (isChoosed1) {
                    if (isKind1) {
                        kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind1) {
                        kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed2) {
                    if (isKind2) {
                        kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind2) {
                        kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed3) {
                    if (isKind3) {
                        kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow_s);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }
                } else {
                    if (isKind3) {
                        kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }

                if (isChoosed4) {
                    if (isKind4) {
                        kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow_s);
                    }
                } else {
                    if (isKind4) {
                        kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                        kind1IV.setImageResource(R.mipmap.tab_arrow);
                        kind2IV.setImageResource(R.mipmap.tab_arrow);
                        kind3IV.setImageResource(R.mipmap.tab_arrow);
                        kind4IV.setImageResource(R.mipmap.tab_arrow);
                    }

                }


            }
        });
    }

    @Override
    protected void initListener() {

        iv_back.setOnClickListener(this);
        iv_search.setOnClickListener(this);

        kind1TV.setOnClickListener(this);
        kind2TV.setOnClickListener(this);
        kind3TV.setOnClickListener(this);
        kind4TV.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);


    }

    @Override
    protected void initData() {
        ArticleKindDao kindDao = new ArticleKindDao(mContext);
        kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), "1");

        System.out.println("kindNews=====" + kindNews);

        if (kindNews.size() == 0) {
            kindLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.GONE);
        }

        isKinds = new boolean[kindNews.size()];
        for (int i = 0; i < kindNews.size(); i++) {
            isKinds[i] = false;
            kindNews.get(i).setIsSelected(false);
        }

        if (kindNews.size() < 5) {
            kindLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
            ll_kind1.setVisibility(View.GONE);
            ll_kind2.setVisibility(View.GONE);
            ll_kind3.setVisibility(View.GONE);
            ll_kind4.setVisibility(View.GONE);
            view1.setVisibility(View.GONE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
            switch (kindNews.size()) {
                case 4:
                    ll_kind4.setVisibility(View.VISIBLE);
                    kind4TV.setText(kindNews.get(3).getName());
                    view1.setVisibility(View.VISIBLE);
                    view2.setVisibility(View.VISIBLE);
                    view3.setVisibility(View.VISIBLE);
                case 3:
                    ll_kind3.setVisibility(View.VISIBLE);
                    kind3TV.setText(kindNews.get(2).getName());
                    view1.setVisibility(View.VISIBLE);
                    view2.setVisibility(View.VISIBLE);
                case 2:
                    ll_kind2.setVisibility(View.VISIBLE);
                    kind2TV.setText(kindNews.get(1).getName());
                    view1.setVisibility(View.VISIBLE);
                case 1:
                    ll_kind1.setVisibility(View.VISIBLE);
                    kind1TV.setText(kindNews.get(0).getName());
                    break;
            }

        } else {
            kindLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);

            //设置适配器

            mAdapter = new IvgTextAdapter(this, kindNews, this);
            mRecyclerView.setAdapter(mAdapter);

        }
        loading();
        YiJiaHuNetWorkUtils.newarticleList(loginInfo.getDepartmentId(), "", "", type, pageIndex, pageSize, handler);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 20) {
                String id = data.getStringExtra("id");
                String name = data.getStringExtra("name");
                Intent iBack = new Intent();
                iBack.putExtra("id", id);
                iBack.putExtra("name", name);
                setResult(RESULT_OK, iBack);
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.iv_title_func:
                Intent searchIntent = new Intent(NewNewsActivity.this, SearchNewsActivity.class);
                searchIntent.putExtra("type", type);
                if (isChoose) {
                    searchIntent.putExtra("isChoose", isChoose);
                    startActivityForResult(searchIntent, 20);
                } else {
                    startActivity(searchIntent);
                }
                break;
            case R.id.news_tv_kind1:

                kind1IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));

                if (isKind1) {
                    isKind2 = false;
                    isKind3 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(0);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.news_tv_kind2:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow);

                kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));

                if (isKind2) {
                    isKind1 = false;
                    isKind3 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(1);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.news_tv_kind3:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind4IV.setImageResource(R.mipmap.tab_arrow);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.black_text));

                if (isKind3) {
                    isKind1 = false;
                    isKind2 = false;
                    isKind4 = false;
                } else {
                    getKindChilds(2);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.news_tv_kind4:
                kind1IV.setImageResource(R.mipmap.tab_arrow);
                kind2IV.setImageResource(R.mipmap.tab_arrow);
                kind3IV.setImageResource(R.mipmap.tab_arrow);
                kind4IV.setImageResource(R.mipmap.tab_arrow_pre);
                kind1TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind2TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind3TV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                kind4TV.setTextColor(mContext.getResources().getColor(R.color.light_blue));

                if (isKind4) {
                    isKind1 = false;
                    isKind3 = false;
                    isKind2 = false;
                } else {
                    getKindChilds(3);
                }
                if(kindChilds!=null){
                    for (int i = 0; i < kindChilds.size(); i++) {
                        if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                            kindChilds.get(i).setIsChoosed(true);
                        } else {
                            kindChilds.get(i).setIsChoosed(false);
                        }
                    }
                }
                kindAdapter.setData(kindChilds);
                kindPopup.showAsDropDown(kindLayout);
                break;
            case R.id.kind_view_whole:
                if (kindPopup != null) {
                    kindPopup.dismiss();
                }
                if (isKinds != null) {
//                    for(int i = 0;i < isKinds.length;i++){
//                        if(i != position){
//                            isKinds[i] = false;
//                        }
//                    }
                }

        }

    }

    public void getKindChilds(int postion) {
        switch (postion) {
            case 0:
                isKind1 = true;
                isKind4 = false;
                isKind3 = false;
                isKind2 = false;
                break;
            case 1:
                isKind2 = true;
                isKind4 = false;
                isKind3 = false;
                isKind1 = false;
                break;
            case 2:
                isKind3 = true;
                isKind4 = false;
                isKind2 = false;
                isKind1 = false;
                break;
            case 3:
                isKind4 = true;
                isKind3 = false;
                isKind2 = false;
                isKind1 = false;
                break;
        }
        kindChilds.clear();
//        ArticleKindDao kindDao = new ArticleKindDao(mContext);
//        kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), type);
        ArticleKindChildBean bean = new ArticleKindChildBean();
        bean.setName(getResources().getString(R.string.usermanger_text_all));
        bean.setItemId(kindNews.get(postion).getItemId());
        kindChilds.add(bean);
        kindChilds.addAll(kindNews.get(postion).getChildList());
    }


    @Override
    public void onSelected(String type, ArticleBean article) {

        if ("click".equals(type)) {
            if (isChoose) {
                Intent iBack = new Intent();
                iBack.putExtra("id", article.getArticleId());
                iBack.putExtra("name", article.getName());
                setResult(RESULT_OK, iBack);
                finish();
            } else {
                Intent intent = new Intent(NewNewsActivity.this, ArticleDetailActivity.class);
                intent.putExtra("articleId", article.getArticleId());
                intent.putExtra("name", article.getName());
                intent.putExtra("acctType", this.type);
//                if(kindNews.size()>4){
//                    for (int i = 0; i < kindNews.size(); i++) {
//                        if(kindNews.get(i).isSelected()){
//                            intent.putExtra("kindTitle",  kindNews.get(i).getName());
//                            break;
//                        }
//                    }
//                }else{
//                    if(isChoosed1){
//                        intent.putExtra("kindTitle",  kindNews.get(0).getName());
//                    }else if(isChoosed2){
//                        intent.putExtra("kindTitle",  kindNews.get(1).getName());
//                    }else if(isChoosed3){
//                        intent.putExtra("kindTitle",  kindNews.get(2).getName());
//                    }else if(isChoosed4){
//                        intent.putExtra("kindTitle",  kindNews.get(3).getName());
//                    }
//                }
                intent.putExtra("kindTitle",  article.getKindName());

                startActivity(intent);
            }
            //收藏    0收藏1取消收藏
//            YiJiaHuNetWorkUtils.collectArtical(article.getArticleId(), "0", handler);
            //评论
//            YiJiaHuNetWorkUtils.commentArtical(article.getArticleId(),"123456",handler);
        }

    }

    @Override
    public void onSelected(String selectType, int position) {


        for (int i = 0; i < kindNews.size(); i++) {
            if (i == position) {
                kindNews.get(position).setIsSelected(true);
            } else {
                kindNews.get(i).setIsSelected(false);
            }
        }
        mAdapter.setData(kindNews);

        if (isKinds[position]) {
            for (int i = 0; i < isKinds.length; i++) {
                if (i != position) {
                    isKinds[i] = false;
                }
            }
        } else {
            for (int i = 0; i < isKinds.length; i++) {
                isKinds[i] = false;
            }
            isKinds[position] = true;
            kindChilds.clear();
//            ArticleKindDao kindDao = new ArticleKindDao(mContext);
//            kindNews = kindDao.getKinds(loginInfo.getDepartmentId(), type);
            ArticleKindChildBean bean = new ArticleKindChildBean();
            bean.setName(getResources().getString(R.string.usermanger_text_all));
            bean.setItemId(kindNews.get(position).getItemId());
            kindChilds.add(bean);
            kindChilds.addAll(kindNews.get(position).getChildList());

        }
        if(kindChilds!=null){
            for (int i = 0; i < kindChilds.size(); i++) {
                if (selectChildId.equals(kindChilds.get(i).getItemId())) {
                    kindChilds.get(i).setIsChoosed(true);
                } else {
                    kindChilds.get(i).setIsChoosed(false);
                }
            }
        }
        kindAdapter.setData(kindChilds);
        kindPopup.showAsDropDown(mRecyclerView);

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        isLoadMore = false;
        pageIndex = 1;
        newsInfoList.clear();
        loading();
        YiJiaHuNetWorkUtils.newarticleList(loginInfo.getDepartmentId(), "", "", type, pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

        if (newsInfoList.size() == totalCount) {
            isLoadMore = false;
            refreshLayout
                    .loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }

        isLoadMore = true;
        pageIndex++;
        loading();
        YiJiaHuNetWorkUtils.newarticleList(loginInfo.getDepartmentId(), "", "", type, pageIndex, pageSize, handler);

    }
}
